#include"BlockQueue.hpp"

//想让创建的新线程看见阻塞队列，传参进入让其看见
struct ThreadData
{
    BlockQueue<func_t>* bq;
    std::string name;
};

void* consumer(void * args)
{
    ThreadData* ctd = static_cast<ThreadData*>(args);
    //从阻塞队列中获取数据、任务执行
    while(true)
    {
        func_t f;
        ctd->bq->Pop(&f);
        f();
        sleep(1);
    }
    return (void*)0;
}

void* productor(void* args)
{
    ThreadData* ptd = static_cast<ThreadData*>(args);
    //向阻塞队列中放任务
    while(true)
    {
        ptd->bq->Enqueue(PrintLog);
        std::cout << "生产者生产了一个任务" <<std::endl;
    }
    return (void*)0;
}

int main()
{
    BlockQueue<func_t>* bq = new BlockQueue<func_t>();
    //主线程创建新线程
    pthread_t c,p;
    ThreadData ctd = { bq, "消费者"};
    pthread_create(&c,nullptr,consumer , (void*)&ctd);

    ThreadData ptd = {bq , "生产者"};
    pthread_create(&p, nullptr, productor , (void*)&ptd);

    //主线程需要等待新线程
    pthread_join(c,nullptr);
    pthread_join(p , nullptr);
    return 0;
}